package com.uniandes.edu.co.interfaz.tarea;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;

import com.uniandes.edu.co.dto.Ciclo;
import com.uniandes.edu.co.dto.Persona;
import com.uniandes.edu.co.dto.Tarea;
import com.uniandes.edu.co.dto.Tsp;
import com.uniandes.edu.co.excepciones.ExcepcionVacio;
import com.uniandes.edu.co.interfaz.mundo.Principal;




/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
@SuppressWarnings("serial")
//public class InterfazConsultarTarea extends javax.swing.JFrame {
public class InterfazConsultarTarea extends JDialog {

	
	private Principal principal;
	
	private JLabel jLabel1;
	private JButton jButton1;
	private JComboBox jComboBox2;
	private JComboBox jComboBox1;
	private JButton jButton2;
	private JTextArea jTextArea1;
	private JScrollPane jScrollPane1;
	private JLabel jLabel3;
	private JLabel jLabel2;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	/*public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				InterfazConsultarTarea inst = new InterfazConsultarTarea( );
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public InterfazConsultarTarea() {
		super();
		initGUI( );
	}*/
	
	public InterfazConsultarTarea(Principal id) {
		super(id, true);
	    principal = id;
	    setTitle( "Consultar Tarea" );
	    initGUI();
	}

	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1);
				jLabel1.setText("Consultar Tarea");
				jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
				jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);
				jLabel1.setFont(new java.awt.Font("Segoe UI",1,14));
				jLabel1.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
				jLabel1.setBounds(170, 12, 327, 29);
			}
			{
				jButton1 = new JButton();
				getContentPane().add(jButton1);
				jButton1.setText("Regresar");
				jButton1.setBounds(535, 350, 112, 23);
				jButton1.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						dispose();
																		
							//Principal.main(null);
						
					}
				});
			}
			{
				jLabel2 = new JLabel();
				getContentPane().add(jLabel2);
				jLabel2.setText("Id Tarea");
				jLabel2.setBounds(58, 59, 82, 20);
				jLabel2.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
			}
			{
				jLabel3 = new JLabel();
				getContentPane().add(jLabel3);
				jLabel3.setText("Ciclo");
				jLabel3.setBounds(367, 59, 82, 20);
				jLabel3.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
			}
			{
				jScrollPane1 = new JScrollPane();
				getContentPane().add(jScrollPane1);
				jScrollPane1.setBounds(58, 137, 584, 187);
				{
					jTextArea1 = new JTextArea();
					jScrollPane1.setViewportView(jTextArea1);
				}
			}
			{
				jButton2 = new JButton();
				getContentPane().add(jButton2);
				jButton2.setText("Consultar");
				jButton2.setBounds(526, 103, 116, 23);
				jButton2.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						String idTareaSeleccionada=(String)jComboBox1.getSelectedItem();
						String idCicloSeleccionado=(String)jComboBox2.getSelectedItem();
						consultarTarea(idTareaSeleccionada, idCicloSeleccionado);
					}
				});
			}
			{
				List <Ciclo> ciclos=Tsp.getSingleton().getCiclos();
				List <String> idCiclos=new ArrayList<String>();
				idCiclos.add("Todos");
				for (Ciclo cic : ciclos) {
					idCiclos.add(String.valueOf(cic.getIdCiclo()));
				}
				ComboBoxModel jComboBox2Model = 
					new DefaultComboBoxModel(
							idCiclos.toArray());
				jComboBox2 = new JComboBox();
				getContentPane().add(jComboBox2);
				jComboBox2.setModel(jComboBox2Model);
				jComboBox2.setBounds(467, 58, 175, 23);
				jComboBox2.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						llenarTareas();
					}
				});
			}
			{
				List <Ciclo> ciclos=Tsp.getSingleton().getCiclos();
				String idCicloSeleccionado=(String)jComboBox2.getSelectedItem();
				List <String> idTarea=new ArrayList<String>();
				idTarea.add("Todos");
				for (Ciclo cic : ciclos) {
					if(idCicloSeleccionado.equalsIgnoreCase(String.valueOf(cic.getIdCiclo()))){
						List<Tarea> listaTareas=cic.getTareas();
						for (Tarea tar : listaTareas) {
							idTarea.add(tar.getIdTarea());
						}
					}
				}
				ComboBoxModel jComboBox1Model = 
					new DefaultComboBoxModel(
							idTarea.toArray());
				jComboBox1 = new JComboBox();
				getContentPane().add(jComboBox1);
				jComboBox1.setModel(jComboBox1Model);
				jComboBox1.setBounds(170, 58, 156, 23);
			}
			pack();
			this.setSize(699, 428);
			centrarVentana();
		} catch (Exception e) {
			//add your error handling code here
			e.printStackTrace();
		}
	}
	
    private void centrarVentana(){
        Dimension dPantalla = Toolkit.getDefaultToolkit( ).getScreenSize( );
        Dimension dVentana = getSize( );

        int xEsquina = ( dPantalla.width / 2 ) - ( dVentana.width / 2 );
        int yEsquina = ( dPantalla.height / 2 ) - ( dVentana.height / 2 );

        setLocation( xEsquina, yEsquina );
    }
    
    private void llenarTareas(){
    	List <Ciclo> ciclos=Tsp.getSingleton().getCiclos();
		String idCicloSeleccionado=(String)jComboBox2.getSelectedItem();
		List <String> idTarea=new ArrayList<String>();
		idTarea.add("Todos");
		for (Ciclo cic : ciclos) {
			if(idCicloSeleccionado.equalsIgnoreCase(String.valueOf(cic.getIdCiclo()))){
				List<Tarea> listaTareas=cic.getTareas();
				for (Tarea tar : listaTareas) {
					idTarea.add(tar.getIdTarea());
				}
			}
		}
		ComboBoxModel jComboBox3Model = 
			new DefaultComboBoxModel(
					idTarea.toArray());
		jComboBox1.setModel(jComboBox3Model);
		
    }
    
    private void consultarTarea(String idTarea, String idCiclo){
    	try{
    		String texto=Tsp.getSingleton().consultarTareasCiclo(idTarea, idCiclo);
        	jTextArea1.setText(texto);
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    }

}
